﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FORUs
{
    public partial class Frm02_EditPlaylist : Form
    {
        
        #region Local Variables
        UltrastarPlaylist _uPlaylist;
        #endregion

        #region Constructors
        public Frm02_EditPlaylist(UltrastarPlaylist playlist)
        {
            InitializeComponent();

            // Loading the Playlist
            _uPlaylist = playlist;
            LoadPlaylist();

            _trvSongs.Mode = SongsTreeview.DisplayMode.None;
            _trvSongs.SongDirectory = Program.SongDirectory;
            _trvSongs.DisplaySongs();
        }
        #endregion
               
        #region Events - Playlist's Buttons
        private void _btnSave_Click(object sender, EventArgs e)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            if (!_txtPlaylistName.Text.Equals(string.Empty))
                _uPlaylist.Name = _txtPlaylistName.Text;

            if (!_uPlaylist.Save())
                MessageBox.Show(string.Concat("Erreur Lors de la sauvegarde du fichier ", _uPlaylist.Filename),
                    "Erreur de sauvegarde", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show("Playlist sauvegardée !",
                    "Sauvegarde", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Cursor.Current = current;
            
        }

        private void _btnCancel_Click(object sender, EventArgs e)
        {
            // Resetting the playlist as original
            if (File.Exists(_uPlaylist.Filename))
                _uPlaylist = UltrastarPlaylist.FromFile(_uPlaylist.Filename);
            else
                _uPlaylist = UltrastarPlaylist.CreateTo(_uPlaylist.Filename, _uPlaylist.Name);
            
            LoadPlaylist();
        }
        #endregion

        #region Events - Adding/Removing songs from playlist
        private void _btnAdd_Click(object sender, EventArgs e)
        {
            // Getting the selected song in the treeview
            UltrastarSong usSong = _trvSongs.SelectedSong;
            if (usSong.IsEmpty)
                return;

            // Adding the song to the playlist
            if (_uPlaylist.AddSong(usSong))
                _uPlaylist.LoadIntoListBox(_lstPlaylist);
        }
          
        private void _btnRemove_Click(object sender, EventArgs e)
        {
            int index = _lstPlaylist.SelectedIndex;
            if (index < 0)
                return;

            _uPlaylist.RemoveSongAt(index);
            _lstPlaylist.Items.RemoveAt(index);

        }
        #endregion

        #region Playlist Functions
        private void LoadPlaylist()
        {
            if (!_uPlaylist.IsEmpty)
            {
                _txtPlaylistName.Text = _uPlaylist.Name;
                _uPlaylist.LoadIntoListBox(_lstPlaylist);
            }
            else
            {
                _txtPlaylistName.Text = "";
                _lstPlaylist.Items.Clear();
                MessageBox.Show("Erreur dans le chargement de la playlist");
            }
        }
        #endregion

    }
}
